package com.gc.business.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gc.business.domain.AdminRegion;
import com.gc.business.mapper.AdminRegionMapper;
import com.gc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gc.business.mapper.GovRegionMapper;
import com.gc.business.domain.GovRegion;
import com.gc.business.service.IGovRegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

/**
 * 政府管理区域中间 服务层实现
 * 
 * @author hruiyan
 * @date 2020-11-12
 */
@Slf4j
@Service
public class GovRegionServiceImpl extends ServiceImpl<GovRegionMapper, GovRegion> implements IGovRegionService {

    @Autowired
    private AdminRegionMapper adminRegionMapper;

    @Override
    public List<GovRegion> list(GovRegion govRegion){
        LambdaQueryWrapper<GovRegion> wrapper = new LambdaQueryWrapper<>();
        if(govRegion != null){
            if(StringUtils.isNotEmpty(govRegion.getId())){
                wrapper.eq(GovRegion::getId, govRegion.getId());
            }
            if(StringUtils.isNotEmpty(govRegion.getGovId())){
                wrapper.eq(GovRegion::getGovId, govRegion.getGovId());
            }
            if(StringUtils.isNotEmpty(govRegion.getRegionId())){
                wrapper.eq(GovRegion::getRegionId, govRegion.getRegionId());
            }
        }
        return baseMapper.selectList(wrapper);
    }

    /**
     * 根据政府机构ID查询
     * @param govId
     * @return
     */
    @Override
    public List<AdminRegion> selectGovRegionByGovId(Long govId){
        List<GovRegion> regionListByGovId = baseMapper.selectGovRegionByGovId(govId);
        List<AdminRegion> regionList = adminRegionMapper.selectAdminRegion();

        for (GovRegion govRegionByGovId : regionListByGovId) {
            for (AdminRegion govRegion : regionList) {
                if(govRegionByGovId.getRegionId().longValue() == govRegion.getRegionId().longValue()){
                    govRegion.setFlag(true);
                    break;
                }
            }
        }
        return regionList;
    }


    /**
     * 根据政府机构ID删除保存过的行政区域数据
     * @param govId
     * @return
     */
    @Override
    public int deleteGovRegionByGovId(Long govId){
        return baseMapper.deleteGovRegionByGovId(govId);
    }

}
